package cn.tips.dmp.supports.jpa;

import lombok.Data;
import org.apache.commons.lang3.EnumUtils;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

import java.util.Arrays;

/**
 * @author pkb
 */
@Data
public class SortParamsDTO {
    private String[] sortFields;
    private String sortDirection;

    public Sort getSort() {
        if (null != this.sortFields && this.sortFields.length >= 1 && !StringUtils.isEmpty(this.sortDirection)) {
            assert EnumUtils.isValidEnum(Sort.Direction.class, this.sortDirection.toUpperCase());

            return Sort.by((Sort.Direction)EnumUtils.getEnum(Sort.Direction.class, this.sortDirection.toUpperCase()), this.sortFields);
        } else {
            return null;
        }
    }

    public SortParamsDTO() {
    }
}
